package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserCardList;
import com.health.db.model.UserCardListExt;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserCardListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UserApplyRefoundReq;
import com.health.mapp.bean.UserApplyRefoundResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;


/**
 * 退款操作类
 * 首先的问题，退款正常流程下是否要审核
 *  如果不需要审核，则此类需要完成从医生账户中扣减相应费用并将费用返还给用户,则退款中状态也不会出现，直接到订单关闭状态
 *  如果需要审核，此类只做订单状态修改，则会出现退款中状态
 * @author acer
 *
 */
public class UserApplyRefoundImpl  	 implements ActionInterface{


			
			//service import
			private IRegisterOrderService registerOrderService ;
			private IUserLISTService userLISTService  ;
			private IUserAccountLedgerService userAccountLedgerService  ;
			private IUserCardListService userCardListService;
			
			@Override
			public Response execute(Header header, Object reqBean) throws Exception {
				
				UserApplyRefoundReq req = (UserApplyRefoundReq) reqBean; 
				UserApplyRefoundResp _resp = new UserApplyRefoundResp();
				Long orderId = req.getOrderId();
				Long docId= req.getDocId();
				Long userId = req.getUserId();
				if (userId==0 || orderId==0 ) {
					_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				}else{
					//RegisterOrder orderInfo = new RegisterOrder();
					
				 
					RegisterOrder  currRegisterOrder  =registerOrderService.selectSimpleSingleByOrderId(orderId);
					
					//判断订单状态
					if (currRegisterOrder==null) {
						_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
						_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
						return _resp;
					}
					
					//判断类型，状态必须是支付过和订单未完成前才可以操作
					if (currRegisterOrder.getRunstatusid().intValue()==Constant.CONSTANTS_ORDER_RUNSTATUS_ID_1.intValue()
							|| currRegisterOrder.getRunstatusid().intValue()==Constant.CONSTANTS_ORDER_RUNSTATUS_ID_5.intValue()
							||currRegisterOrder.getRunstatusid().intValue()==Constant.CONSTANTS_ORDER_RUNSTATUS_ID_6.intValue()
							|| currRegisterOrder.getRunstatusid().intValue()==Constant.CONSTANTS_ORDER_RUNSTATUS_ID_7.intValue()) {
						_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3004);
						_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3004_NOTE);
						return _resp;
					}
					//付款方式
					short payFlag=currRegisterOrder.getPayTypeId();
					int registerType= currRegisterOrder.getRegistertypeid().intValue();
					currRegisterOrder.setRunStatus(Constant.CONSTANTS_ORDER_RUNSTATUS_DES_6);
					currRegisterOrder.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_6);
					 Date currDateTime=DateUtils.getDatetime();
					 
						currRegisterOrder.setUpdateDate(currDateTime);
					
					//注意，目前允许退款的订单类型是等待就诊和就诊中两个状态。所以钱并未到医生账户，至于订单为完成状态时，是否允许退款需要讨论(按淘宝模式是不允许的)
					//用户服务卡支付
					if(payFlag==Constant.CONSTANTS_PAYMENT_SERVICE_CARD)
					{
						//图文咨询
						if (currRegisterOrder.getRegistertypeid().intValue()==Constant.CONSTANTS_ORDER_TYPE_ID_1.intValue()) {
							 
							long cardId=Long.valueOf(currRegisterOrder.getCardID());
							//查到具体是哪张卡里扣减的
							UserCardList  userCardList =userCardListService.selectByCardId(cardId);
							
							if(null!=userCardList){
							
							userCardList.setLefttime01(userCardList.getLefttime01().add(new BigDecimal(1)));
							registerOrderService.updateOrder4CardRefound(currRegisterOrder, userCardList);
							}else{
								//信息错误
								_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
								_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
								return _resp;
							}
						}
						else
						{
							String[] cardIds=currRegisterOrder.getCardID().split(",");
							//多条批处理
							if (cardIds.length==1) {
								//查到具体是哪张卡里扣减的
								UserCardList  userCardList =userCardListService.selectByCardId(Long.valueOf(currRegisterOrder.getCardID()));
								
								if(null!=userCardList){
								
									userCardList.setLefttime01(userCardList.getLefttime01().add(new BigDecimal(1)));
									registerOrderService.updateOrder4CardRefound(currRegisterOrder, userCardList);
								}else{
									//信息错误
									_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
									_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
									return _resp;
								}
							}else{
								
								String[] cardLeftTime=currRegisterOrder.getCardJournalAmount().split(",");
								
								UserCardListExt example = new UserCardListExt();
								example.setOwnerid(currRegisterOrder.getUserid());
								//查询所有的cardList
								List<UserCardList> userCardLists =userCardListService.selectUserCardLists(example);
								//需要更新的表
								List<UserCardList> userCardListsForUpdate = new ArrayList<UserCardList>();
								Map<String,UserCardList> map = new HashMap<String, UserCardList>();
								for (int i = 0; i < userCardLists.size(); i++) {
									UserCardList card = userCardLists.get(i);
									map.put(String.valueOf(card.getCardid()), card);
								}
								
								for (int i = 0; i < cardIds.length; i++) {
									UserCardListExt queryUserCardListExt =new UserCardListExt();
									//查到具体是哪张卡里扣减的
									UserCardList  userCardList =map.get(cardIds[i]);
									 //剩余时间
									userCardList.setLefttime03(userCardList.getLefttime03().add(new BigDecimal(cardLeftTime[i])));
									userCardListsForUpdate.add(userCardList);
								}
								
								registerOrderService.updateOrder4CardRefound(currRegisterOrder, userCardListsForUpdate);
							}
							
						}
					}
					//用户钱包支付
					else if(payFlag==Constant.CONSTANTS_PAYMENT_WALLET)
					{
						//用户账户表
			 
 
						//查询到当前用户的账户
						UserAccountLedger accountLedger = new UserAccountLedger();
						accountLedger = userAccountLedgerService.selectByUserId(userId);
					 
						//需要确认订单中的支付方式是哪种
						
						//有账户则直接给账户增加订单金额
						if (null!=accountLedger ) {
							 
							//医生获得的补偿从账户中减少掉
							accountLedger.setBalanceTotal(accountLedger.getBalanceTotal().add(currRegisterOrder.getRegisterPayoffTotal()));
						}
						
						
						//更新订单表，更新账户表
					  RegisterOrderExample registerOrderExample= registerOrderService.updateOrder4WalletRefound(currRegisterOrder);
					
					}
					 
				 
				
					 
					
					 
					 
					
					
					 
					 
				}
				
				_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
				_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
		 
				return _resp;
			}
			
			public IRegisterOrderService getRegisterOrderService() {
				return registerOrderService;
			}


			public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
				this.registerOrderService = registerOrderService;
			}


			public IUserLISTService getUserLISTService() {
				return userLISTService;
			}


			public void setUserLISTService(IUserLISTService userLISTService) {
				this.userLISTService = userLISTService;
			}

			public IUserAccountLedgerService getUserAccountLedgerService() {
				return userAccountLedgerService;
			}

			public void setUserAccountLedgerService(
					IUserAccountLedgerService userAccountLedgerService) {
				this.userAccountLedgerService = userAccountLedgerService;
			}

			public IUserCardListService getUserCardListService() {
				return userCardListService;
			}

			public void setUserCardListService(IUserCardListService userCardListService) {
				this.userCardListService = userCardListService;
			}
			



}
